﻿namespace Rialternative.Repositories
{
    using System;
    using System.Collections.Generic;
    using System.Diagnostics.CodeAnalysis;
    using System.Linq;

    /// <summary>
    /// Generic in memory repository implementing basic methods.
    /// </summary>
    /// <typeparam name="TEntity">The type of the entity.</typeparam>
    public class InMemoryRepository : IRepository
    {
        /// <summary>
        /// The in memory tables.
        /// </summary>
        private Dictionary<Type, IList<object>> tables = new Dictionary<Type, IList<object>>();

        /// <summary>
        /// Initializes a new instance of the <see cref="InMemoryRepository"/> class.
        /// </summary>
        public InMemoryRepository()
        {
        }

        /// <summary>
        /// Queries this instance.
        /// </summary>
        /// <typeparam name="TEntity">The type of the entity.</typeparam>
        /// <returns>A set of entities.</returns>
        public IQueryable<TEntity> GetQuery<TEntity>() where TEntity : class
        {
            return this.GetTable<TEntity>().Cast<TEntity>().AsQueryable();
        }

        /// <summary>
        /// Adds the specified entity.
        /// </summary>
        /// <typeparam name="TEntity">The type of the entity.</typeparam>
        /// <param name="entity">The entity.</param>
        public void Insert<TEntity>(TEntity entity) where TEntity : class
        {
            this.GetTable<TEntity>().Add(entity);
        }

        /// <summary>
        /// Removes the specified entity.
        /// </summary>
        /// <typeparam name="TEntity">The type of the entity.</typeparam>
        /// <param name="entity">The entity.</param>
        public void Delete<TEntity>(TEntity entity) where TEntity : class
        {
            this.GetTable<TEntity>().Remove(entity);
        }

        /// <summary>
        /// Updates the specified entity.
        /// </summary>
        /// <typeparam name="TEntity">The type of the entity.</typeparam>
        /// <param name="entity">The entity.</param>
        /// <param name="original">The original version of the entity.</param>
        public void Update<TEntity>(TEntity entity, TEntity original) where TEntity : class
        {
        }

        /// <summary>
        /// Persists changes.
        /// </summary>
        public void Persist()
        {
        }

        /// <summary>
        /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
        /// </summary>
        public void Dispose()
        {
            this.Dispose(true);
            GC.SuppressFinalize(this);
        }

        /// <summary>
        /// Releases unmanaged and - optionally - managed resources.
        /// </summary>
        /// <param name="disposing">Release both managed and unmanaged resources if <c>true</c>; <c>false</c> to release only unmanaged resources.</param>
        protected virtual void Dispose(bool disposing)
        {
        }

        /// <summary>
        /// Gets the table for the specified entity type.
        /// </summary>
        /// <typeparam name="TEntity">The type of the entity.</typeparam>
        /// <returns>The table for the specified entity type.</returns>
        [SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter", Justification = "GetTable needs the type of the entity.")]
        protected IList<object> GetTable<TEntity>() where TEntity : class
        {
            if (!this.tables.ContainsKey(typeof(TEntity)))
            {
                this.tables[typeof(TEntity)] = new List<object>();
            }

            return this.tables[typeof(TEntity)];
        }
    }
}
